package com.fast.develop.meta.dm;

import com.fast.develop.meta.base.MetaRegistor;
import com.fast.develop.meta.base.reader.DomainSchemaFileReader;
import com.fast.develop.meta.base.reader.SchemaReader;
import com.fast.develop.meta.base.resource.MetaFileMultResource;
import com.fast.develop.meta.dm.loader.MetaModelLoader;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author 莫寅
 * @createTime 2022-06-15 14:54
 * @desc：
 */
public class MetaDomainRegistor implements MetaRegistor {

    private static final String DEFAULT_PATH = ".schema/domain";

    private static String DEV_DEFAULT_PATH = null;


    static {
        try {
            String path = MetaDomainRegistor.class.getResource("/").getPath();
            if(path.indexOf("/target/classes/") != -1){
                DEV_DEFAULT_PATH = URLDecoder.decode(
                        path.replace("/target/classes/", "/src/main/resources/.schema/domain"),"UTF-8");
            }else if(path.indexOf("/target/test-classes/") != -1){
                DEV_DEFAULT_PATH = DEFAULT_PATH;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    @Override
    public SchemaReader getSchemaReader() {
        return new DomainSchemaFileReader(new MetaFileMultResource(DEV_DEFAULT_PATH), new MetaModelLoader());
    }


    @Override
    public String type() {
        return MetaDomainConstant.TYPE;
    }
}
